package com.common.library.data;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.zs4sdk.common.Extra;
import com.common.library.util.ActivityHelper;
import com.common.library.util.Helper;

public class CommonHandle extends Handler {
	
	Context ctx;
	
	HandleMessage handle;
	
	public CommonHandle(Context ctx, HandleMessage handle) {
		this.ctx = ctx;
		this.handle = handle;
	}
	
	private static ProgressDialog mProgressDialog;
	private static ProgressDialog getProgressDialog(Context ctx){
		if (mProgressDialog == null){
			mProgressDialog = ProgressDialog.show(ctx, "", "", false, true);
		}
		return mProgressDialog;
	}
	
	/**
	 * 
	 * function: 显示progress
	 *
	 * @param ctx
	 * @param title
	 * @param message
	 * @param indeterminate
	 * @param cancelable
	 * @param onceMore
	 * 
	 * @ author: aXiBa 2013-12-4 下午9:50:39
	 */
	private static void showProgressDialog(Context ctx, String title, String message, boolean indeterminate, boolean cancelable, boolean onceMore) {
		if (Helper.isNotNull(ctx)){
//		if (Helper.isNotNull(ctx) && !ctx.isFinishing()){
			try{
				getProgressDialog(ctx).dismiss();
				getProgressDialog(ctx).setTitle(title);
				getProgressDialog(ctx).setMessage(message);
				getProgressDialog(ctx).setIndeterminate(indeterminate);
				getProgressDialog(ctx).setCancelable(cancelable);
				getProgressDialog(ctx).show();
			}catch (Exception e) {
				e.printStackTrace();
				dismissProgressDialog();
				mProgressDialog = null;
				if (onceMore){
					showProgressDialog(ctx, title, message, indeterminate, cancelable, false);
				}
			}
		}
	}
	
	/**
	 * 
	 * function: 隐藏progress
	 *
	 * 
	 * @ author: aXiBa 2013-12-4 下午9:50:53
	 */
	public static void dismissProgressDialog() {
		try{
			if (Helper.isNotNull(mProgressDialog)){
				mProgressDialog.dismiss();
				mProgressDialog = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void handleMessage(Message msg) {
		super.handleMessage(msg);
		// 显示消息
		if(Extra.MessageKey.SHOWMESSAGE == msg.what) {
			ActivityHelper.showToast((String)msg.obj);
		}
		// 显示加载中
		else if(Extra.MessageKey.SHOWPROCESS == msg.what) {
			ActivityHelper.showProgressDialog(ctx, 
					"",
					ActivityHelper.getString(ActivityHelper.getStringResId("zs4gamestring_hint_progress_send_http_request")),
					false, 
					false);
		}
		// 显示加载中
		else if(Extra.MessageKey.SHOWPROCESS_CANCANCEL == msg.what) {
			ActivityHelper.showProgressDialog(ctx, 
					ActivityHelper.getStringResId("zs4gamestring_hint_progress_send_http_request"));
		}
		// 取消加载中
		else if(Extra.MessageKey.DISMISSPROCESS == msg.what) {
			ActivityHelper.dismissProgressDialog();
		}
		else {
			if(null != handle) {
				handle.doHandleMessage(msg);
			}
		}
		if(Extra.MessageKey.SHOWPROCESS_FORACTIVITY == msg.what) {
			showProgressDialog((Context)msg.obj, 
								"", 
								ActivityHelper.getString(ActivityHelper.getStringResId("zs4gamestring_hint_progress_send_http_request")),
								false, 
								false, 
								true);
		}
		else if(Extra.MessageKey.DISMISSPROCESS_FORACTIVITY == msg.what) {
			dismissProgressDialog();
		}
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////需要被重写的接口
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	public interface HandleMessage {
	
		// 处理其它消息
		void doHandleMessage(Message msg);
	
	}
	
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////需要被重写的接口
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
}
